package com.yaorange.module.system.service.impl;

import com.yaorange.module.system.repository.SysUserRoleRepository;
import com.yaorange.module.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/6/16 20:42
 * @description 用户与角色关系业务层
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
  @Autowired
  private SysUserRoleRepository repository;

  /**
   * 为用户赋予角色
   *
   * @param userId
   * @param roleId
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void addUserRole(Long userId, List<Long> roleId) {
    repository.deleteRoleByUserId(userId);
    if (roleId != null && !roleId.isEmpty()) {
      repository.insertRole(userId, roleId);

    }
  }

  /**
   * 根据用户ID获取角色信息
   * @param id
   * @return
   */
  @Override
  public List<Long> selectRoleById(Long id) {
    return repository.selectRoleByUserId(id);
  }

  /**
   * 批量删除，获取到用户与角色关联的数量
   * @param id
   * @return
   */
  @Override
  public Long getTotalByRole(Long id) {
    return repository.getTotal(id);
  }
}
